package com.kingmed.kmss.common.config;

import com.kingmed.kmss.common.utils.StringUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redisson Client的配置
 * 
 *
 */
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedissonAutoConfiguration {

	@Autowired
	private RedisProperties redisProperties;

	@Bean("redissonClient")
	public RedissonClient redissonClient() {
		Config config = new Config();
//		System.out.println("redisProperties.getHost():"+redisProperties.getHost());
//		System.out.println("redisProperties.getPort():"+redisProperties.getPort());
//		System.out.println("redisProperties.getPassword():"+redisProperties.getPassword());
		// sentinel
		if (redisProperties.getSentinel() != null) {
			SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
			sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
			redisProperties.getSentinel().getNodes();
			sentinelServersConfig.addSentinelAddress(StringUtil.join(redisProperties.getSentinel().getNodes(), ","));
			sentinelServersConfig.setDatabase(redisProperties.getDatabase());
			if (redisProperties.getPassword() != null && redisProperties.getPassword().trim().length() > 0) {
				sentinelServersConfig.setPassword(redisProperties.getPassword());
			}
		} else { // single server
			SingleServerConfig singleServerConfig = config.useSingleServer();
			// format as redis://127.0.0.1:7181 or rediss://127.0.0.1:7181 for SSL
			String schema = redisProperties.isSsl() ? "rediss://" : "redis://";
			singleServerConfig.setAddress(schema + redisProperties.getHost() + ":" + redisProperties.getPort());
			singleServerConfig.setDatabase(redisProperties.getDatabase());
			if (redisProperties.getPassword() != null && redisProperties.getPassword().trim().length() > 0) {
				singleServerConfig.setPassword(redisProperties.getPassword());
			}
		}
		return Redisson.create(config);
	}
}
